//
//  GatewayEditController.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/21.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "GatewayEditController.h"
#import "SureView.h"
@interface GatewayEditController ()
<
UITextFieldDelegate
>
@property (nullable, nonatomic, strong) UIView *m_viewBgName;
@property (nullable, nonatomic, strong) UILabel *m_lblName;
@property (nullable, nonatomic, strong) UITextField *m_textName;
@property (nullable, nonatomic, strong) UILabel *m_lblTips;
@property (nullable, nonatomic, strong) SureView *m_sureView;


@end

@implementation GatewayEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    __weak typeof(self) weakSelf = self;
    self.m_sureView.sureBtnClickBlock = ^(){
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if (!strongSelf) {
            debugLogInfo(@"strongSelf nil...");
            return ;
        }
        debugLogInfo(@"m_textName = %@",strongSelf.m_textName.text);
        [strongSelf.gateway modifyGateName:strongSelf.m_textName.text];
        [strongSelf.navigationController popViewControllerAnimated:YES];
    };
}
- (void) setupUI
{
    self.navigationItem.title = @"编辑网关名字";
    [self.view addSubview:self.m_viewBgName];
    [self.m_viewBgName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(10);
        make.height.equalTo(kIphoneSize_Height(55));
    }];
    [self.m_viewBgName addSubview:self.m_lblName];
    [self.m_lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.m_viewBgName);
        make.leading.equalTo(self.m_viewBgName);
        make.width.equalTo(kIphoneSize_Widith(60));
    }];
    [self.m_viewBgName addSubview:self.m_textName];
    [self.m_textName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.trailing.equalTo(self.m_viewBgName);
        make.leading.equalTo(self.m_lblName.trailing);
    }];
    [self.view addSubview:self.m_lblTips];
    [self.m_lblTips mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(kIphoneSize_Widith(20));
        make.top.equalTo(self.m_viewBgName.bottom).offset(10);
    }];
    [self.view addSubview:self.m_sureView];
    [self.m_sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(self.m_lblTips.bottom);
        make.height.equalTo(100);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)setGateway:(XAIGateway *)gateway
{
    _gateway = gateway;
    self.m_textName.text = gateway.name;
}

- (void)valueChange
{
    debugLogInfo(@"textField = %@",self.m_textName.text);
}
#pragma mark - lazy loading
- (UIView *)m_viewBgName
{
    if (!_m_viewBgName) {
        _m_viewBgName = [[UIView alloc]init];
        _m_viewBgName.backgroundColor = [UIColor whiteColor];
    }
    return _m_viewBgName;
}
- (UILabel *)m_lblName
{
    if (!_m_lblName) {
        _m_lblName = [[UILabel alloc]init];
        _m_lblName.text = @"名称";
        _m_lblName.font = [UIFont systemFontOfSize:15];
        _m_lblName.textColor = RGBColor(51, 51, 51);
        _m_lblName.textAlignment = NSTextAlignmentCenter;
    }
    return _m_lblName;
}
- (UITextField *)m_textName
{
    if (!_m_textName) {
        _m_textName = [[UITextField alloc]init];
        [_m_textName addTarget:self action:@selector(valueChange) forControlEvents:UIControlEventEditingChanged];
        _m_textName.font = [UIFont systemFontOfSize:14];
        _m_textName.clearsOnBeginEditing = true;
        _m_textName.textColor = RGBColor(102, 102, 102);
        _m_textName.clearButtonMode = UITextFieldViewModeAlways;
        
    }
    return _m_textName;
}
- (UILabel *)m_lblTips
{
    if (!_m_lblTips) {
        _m_lblTips = [[UILabel alloc]init];
        _m_lblTips.text = @"请设置你的网关名字";
        _m_lblTips.font = [UIFont systemFontOfSize:14];
        _m_lblTips.textColor = RGBColor(102, 102, 102);
        _m_lblTips.backgroundColor = [UIColor clearColor];
    }
    return _m_lblTips;
}
- (SureView *)m_sureView
{
    if (!_m_sureView) {
        _m_sureView = [[SureView alloc]init];
        _m_sureView.title = @"确定";
    }
    return _m_sureView;
}
@end
